import { chromium } from "playwright";
import { PlaywrightAgent } from "@midscene/web/playwright";
import "dotenv/config"; // read environment variables from .env file

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

Promise.resolve(
  (async () => {
    const browser = await chromium.launch({
      headless: true, // 'true' means we can't see the browser window
      args: ["--no-sandbox", "--disable-setuid-sandbox"],
    });

    const page = await browser.newPage();
    await page.setViewportSize({
      width: 1280,
      height: 768,
    });
    await page.goto("http://novel.hctestedu.com");
    await sleep(3000); // 👀 init Midscene agent
    const agent = new PlaywrightAgent(page, {
      screenshotTimeout: 30000, // Increase screenshot timeout to 30 seconds
    });

    await agent.aiAction("点击登录");

    await sleep(3000);

    await agent.aiInput("18511112222", "请输入手机号码");
    await agent.aiInput("123456", "请输入密码");
    await agent.aiAction("点击登录");
    await sleep(3000);
    await agent.aiAssert("手机号或密码错误！");

    await browser.close();
  })()
);
